package com.puman.beautyfarmcounselor.ui.other;


import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import android.widget.Toast;

import com.puman.beautyfarmcounselor.IndexActivity;
import com.puman.beautyfarmcounselor.MainActivity;
import com.puman.beautyfarmcounselor.R;
import com.puman.beautyfarmcounselor.http.AjaxCallBack;
import com.puman.beautyfarmcounselor.ui.core.BaseFragment;
import com.puman.beautyfarmcounselor.ui.core.BaseInferface;
import com.puman.beautyfarmcounselor.util.XTHttp;
import com.puman.beautyfarmcounselor.util.config.GlobalData;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

@EFragment(R.layout.fragment_rank)
public class RankFragment extends BaseFragment implements BaseInferface {



    @ViewById(R.id.webview)
    WebView newsWebview;
    @ViewById(R.id.base_title)
    TextView topTitle;

    private String infoUrl = "";
    private static final String APP_CACAHE_DIRNAME = "/webcache";
    @Override
    @AfterViews
    public void updateView() {
        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
//        topTitle.setText(R.string.rank_title);
        newsWebview.getSettings().setJavaScriptEnabled(true);
        newsWebview.getSettings().setUseWideViewPort(true);
        newsWebview.getSettings().setLoadWithOverviewMode(true);
        WebSettings webSettings = newsWebview.getSettings();
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);

        newsWebview.getSettings().setSupportZoom(true);
        newsWebview.getSettings().setBuiltInZoomControls(true);
        newsWebview.getSettings().setUseWideViewPort(true);
        newsWebview.getSettings().setAllowFileAccess(true);
//        newsWebview.getSettings().setPluginsEnabled(true);

        newsWebview.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        newsWebview.getSettings().setCacheMode(
                WebSettings.LOAD_NO_CACHE); // ���� ����ģʽ
        newsWebview.getSettings().setDomStorageEnabled(true);
        newsWebview.getSettings().setDatabaseEnabled(true);
        String cacheDirPath = MainActivity.getInstace().getFilesDir()
                .getAbsolutePath()
                + APP_CACAHE_DIRNAME;
        newsWebview.getSettings().setDatabasePath(cacheDirPath);
        newsWebview.getSettings().setAppCachePath(cacheDirPath);
        newsWebview.getSettings().setAppCacheEnabled(true);

        // newsWebview.setBarHeight(3);
        newsWebview.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        newsWebview.setWebChromeClient(new WebChromeClient() {

            public void openFileChooser(ValueCallback<Uri> uploadMsg,
                                        String acceptType, String capture) {
                MainActivity.getInstace().mUploadMessage = uploadMsg;
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                intent.setType("image/*");
                getActivity().startActivityForResult(
                        Intent.createChooser(intent, ""),
                        MainActivity.getInstace().FILECHOOSER_RESULTCODE);

            }

            // 3.0 + �����������
            public void openFileChooser(ValueCallback<Uri> uploadMsg,
                                        String acceptType) {
                MainActivity.getInstace().mUploadMessage = uploadMsg;
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                intent.setType("image/*");
                getActivity().startActivityForResult(
                        Intent.createChooser(intent, "��ɲ�����Ҫʹ��"),
                        MainActivity.getInstace().FILECHOOSER_RESULTCODE);
            }

            // Android < 3.0 �����������
            public void openFileChooser(ValueCallback<Uri> uploadMsg) {
                MainActivity.getInstace().mUploadMessage = uploadMsg;
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                intent.setType("image/*");
                getActivity().startActivityForResult(
                        Intent.createChooser(intent, "��ɲ�����Ҫʹ��"),
                        MainActivity.getInstace().FILECHOOSER_RESULTCODE);

            }

        });
        showWeb();
    }

    XTHttp xtHttp = new XTHttp();
    public void showWeb() {
        infoUrl = "http://220.155.0.55/beautyfarm/?uid="+ GlobalData.counselor.getData().getEmp_no()
                +"&time="+System.currentTimeMillis();
        newsWebview.loadUrl(infoUrl);
        xtHttp.get(infoUrl, new AjaxCallBack<Object>() {
            @Override
            public void onStart() {
                super.onStart();
            }

            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }
    @Override
    public void onClick(View view) {

    }
}
